BarEncoder : Barcode and QR code encoder

更新时间:
2024-05-15
下载文档

BarEncoder : Barcode and QR code encoder

The barencoder module provides bar code encode function.

User can use the following code to import the barencoder module.

var barencoder = require('barencoder');

Support

The following shows barencoder module APIs available for each permissions.

 User ModePrivilege Mode
barencoder.defaultOpt
barencoder.encode

BarEncoder Object

barencoder.defaultOpt(width, height, margin, eccLevel)

  • width {Integer} Image width. default: 100.
  • height {Integer} Image height. default: 100.
  • margin {Integer} Image margin, used for Aztec, PDF417, and QRCode only. default: 10.
  • eccLevel {Integer} ECC level, can be [0-8], used for Aztec, PDF417, and QRCode only. default: 2.
  • Returns: {Object} Bar code encode options.

The returned object contains the following members:

  • width {Integer} Image width.
  • height {Integer} Image height.
  • margin {Integer} Image margin.
  • eccLevel {Integer} ECC level, can be [0-8].

barencoder.encode(text, format, path[, opt])

  • text {String} Text which need encoded.
  • format {String} Encoder format.
  • path {String} Output image file path, suffix name can be *.png and *.jpg.
  • opt {Object} Bar code encode option object. default: barencoder.defaultOpt(100, 100, 10, 2).
  • Returns: {Boolean} true means success, false means failure.

format is a string, can be:

ValueDescription
barencoder.FORMAT_AZTECAXTEC encoder(2D, beta).
barencoder.FORMAT_CODABARCODABAR encoder(1D industrial).
barencoder.FORMAT_CODE_39CODE_39 encoder(1D industrial).
barencoder.FORMAT_CODE_93CODE_93 encoder(1D industrial).
barencoder.FORMAT_CODE_128CODE_128 encoder(1D industrial).
barencoder.FORMAT_DATA_MATRIXDATA_MATRIX encoder(2D).
barencoder.FORMAT_EAN_8EAN_8 encoder(1D product).
barencoder.FORMAT_EAN_13EAN_13 encoder(1D product).
barencoder.FORMAT_ITFITF encoder(1D industrial).
barencoder.FORMAT_PDF_417PDF_417 encoder(2D, beta).
barencoder.FORMAT_QR_CODEQR_CODE encoder(2D).
barencoder.FORMAT_UPC_AUPC_A encoder(1D product).
barencoder.FORMAT_UPC_EUPC_E encoder(1D product).

Example

This example show how to use QR Code encoder encode a text to a PNG format image file.

var barencoder = require('barencoder');

var opt = barencoder.defaultOpt(100, 100, 5, 8);

// will generate sylixos_qr.png
barencoder.encode('www.sylixos.com', barencoder.FORMAT_QR_CODE, './sylixos_qr.png', opt);

Example

This example show how to use Code-128 encoder encode a text to a PNG format image file.

var barencoder = require('barencoder');

// will generate sylixos_1d.png
barencoder.encode('www.sylixos.com', barencoder.FORMAT_CODE_128, './sylixos_1d.png');

barencoder.encode(text, format, imageFormat[, opt])

  • text {String} Text which need encoded.
  • format {String} Encoder format.
  • imageFormat {Object} Output image format object.
  • opt {Object} Bar code encode option object. default: barencoder.defaultOpt(100, 100, 10, 2).
  • Returns: {Buffer} Image buffer.

The bar code imageFormat object contains the following members:

  • pixelFormat {Integer} Pixel format.
  • pixelBytes {Integer} Pixel byte size of image buffer. Only RGB format needed.
  • redIndex {Integer} Red index of RGB pixel. Only RGB format needed.
  • greenIndex {Integer} Green index of RGB pixel. Only RGB format needed.
  • blueIndex {Integer} Blue index of RGB pixel. Only RGB format needed.

pixelFormat is a integer, can be:

ValueDescription
barencoder.PIX_FMT_YUV420PYUV pixel format.
barencoder.PIX_FMT_GRAY8Grayscale pixel format.
barencoder.PIX_FMT_RGBRGB pixel format.
barencoder.PIX_FMT_JPEGJPEG pixel format.
barencoder.PIX_FMT_PNGPNG pixel format.

RGB pixel format:

For example RGBA8888:

    0       1       2       3
+-------+-------+-------+-------+
| red   | green |  blue | alpha |
+-------+-------+-------+-------+

pixelBytes is 4.
redIndex is 0.
greenIndex is 1.
blueIndex is 2.

For example RGB888:

    0       1       2 
+-------+-------+-------+
| red   | green |  blue | 
+-------+-------+-------+

pixelBytes is 3.
redIndex is 0.
greenIndex is 1.
blueIndex is 2.

Example

This example show how to use QR Code encoder encode a text to a RGB888 format image buffer.

var rgb888Buf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE, 
  { pixelFormat: barencoder.PIX_FMT_RGB, pixelBytes: 3, redIndex: 0, greenIndex: 1, blueIndex: 2 }, 
  { width: 100, height: 100, margin: 10 }); 

console.log(rgb888Buf.length);

This example show how to use QR Code encoder encode a text to a YUV format image buffer.

var yuvBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE, 
  { pixelFormat: barencoder.PIX_FMT_YUV420P }, 
  { width: 100, height: 100, margin: 10 }); 

console.log(yuvBuf.length);

This example show how to use QR Code encoder encode a text to a Grayscale format image buffer.

var grayscaleBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE, 
  { pixelFormat: barencoder.PIX_FMT_GRAY8 }, 
  { width: 100, height: 100, margin: 10 }); 

console.log(grayscaleBuf.length);

This example show how to use QR Code encoder encode a text to a JPEG format image buffer.

var jpegBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE, 
  { pixelFormat: barencoder.PIX_FMT_JPEG }, 
  { width: 100, height: 100, margin: 10 }); 

console.log(jpegBuf.length);

This example show how to use QR Code encoder encode a text to a PNG format image buffer.

var pngBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE,
  { pixelFormat: barencoder.PIX_FMT_PNG }, 
  { width: 100, height: 100, margin: 10 }); 

console.log(pngBuf.length);
文档内容是否对您有所帮助?
有帮助
没帮助